package com.jiayang.hhotel.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.ContractListModel;
import com.jiayang.hhotel.bean.ContractListModel;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/5/26 0016.
 * 合同列表适配器
 */

public class ContractListAdapter extends SwipeMenuAdapter<ContractListAdapter.ViewHolder> {

//    private static final int TYPE_ITEM =0; //普通Item View
//    private static final int TYPE_FOOTER = 1; // 底部FootView

    private List<ContractListModel> mList;
    private Context t;

    private btClickListener listener;

    public ContractListAdapter(Context context, List<ContractListModel> list, btClickListener listener){
        this.mList=list;
        this.t=context;
        this.listener=listener;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ContractListModel model=mList.get(position);
        holder.rmNum.setText(t.getString(R.string.room_num)+model.getId());
        holder.startDate.setText(t.getString(R.string.room_name)+model.getStartDate());
        holder.endDate.setText(t.getString(R.string.person_count)+model.getEndDate());
        holder.describe.setText(model.getDescribe());
        holder.bind(model,position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(t).inflate(R.layout.contract_list_item, parent, false);
    }

    @Override
    public ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ViewHolder(realContentView);
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView rmNum;//编号
        TextView startDate;//开始日期
        TextView endDate;//结束日期
        TextView describe;//描述

        Button contractTime;//合同时间
        Button contractRoomPrice;//合同房价
        Button contractDiscount;//合同折扣
        Button renew;//续约
        Button details;//查看详情

        public ViewHolder(View convertView) {
            super(convertView);
            rmNum= (TextView) convertView.findViewById(R.id.tv_id);
            startDate= (TextView) convertView.findViewById(R.id.tv_start_date);
            endDate= (TextView) convertView.findViewById(R.id.tv_end_time);
            describe= (TextView) convertView.findViewById(R.id.tv_describe);

            contractTime= (Button) convertView.findViewById(R.id.bt_contract_time);
            contractRoomPrice= (Button) convertView.findViewById(R.id.bt_contract_room_price);
            contractDiscount= (Button) convertView.findViewById(R.id.bt_contract_discount);
            renew= (Button) convertView.findViewById(R.id.bt_renew);
            details= (Button) convertView.findViewById(R.id.bt_see_details);
        }

        private int position;

        private void bind(ContractListModel model,int pos){
            this.position=pos;
            renew.setTag(model);
            details.setTag(model);

            contractTime.setOnClickListener(this);
            contractRoomPrice.setOnClickListener(this);
            contractDiscount.setOnClickListener(this);
            renew.setOnClickListener(this);
            details.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.bt_contract_time:
                    if(listener!=null){
                        listener.onBTClickListener((ContractListModel) v.getTag(), R.id.bt_contract_time,position);
                    }
                    break;
                case R.id.bt_contract_room_price:
                    if(listener!=null){
                        listener.onBTClickListener((ContractListModel) v.getTag(), R.id.bt_contract_room_price,position);
                    }
                    break;
                case R.id.bt_contract_discount:
                    if(listener!=null){
                        listener.onBTClickListener((ContractListModel) v.getTag(), R.id.bt_contract_discount,position);
                    }
                    break;
                case R.id.bt_see_details:
                    if(listener!=null){
                        listener.onBTClickListener((ContractListModel) v.getTag(), R.id.bt_see_details,position);
                    }
                    break;
                case R.id.bt_renew:
                    if(listener!=null){
                        listener.onBTClickListener((ContractListModel) v.getTag(), R.id.bt_renew,position);
                    }
                    break;
            }
        }
    }

    public interface btClickListener{
        void onBTClickListener(ContractListModel model, int id, int position);
    }
}
